/* Copyright (C) 2011. ChengDu Unicon System, All Rights Reserved */ 

/*""FILE COMMENT""******************************* Technical reference data ****
* File Name	: hwsetup.c
* Version 	: 1.00
* Device 	: SH72543
* Tool Chain 	: 
* H/W Platform	: SH72543
* Description 	: Contains hardware setup functions
******************************************************************************/ 

/******************************************************************************
* History 	: 31.05.2011 Ver. 1.00 First Release
*		: 
*""FILE COMMENT END""*********************************************************/ 

/******************************************************************************
System Includes
******************************************************************************/ 
/* Following header file provides access to intrinsic functions to control 
interrupt levels. */ 
#include <intrinsics.h>

/******************************************************************************
User Includes (Project Level Includes)
******************************************************************************/ 
/* Following header file provides a structure to access on-chip I/O 
registers. */ 
#include "io7254R.h"
/* Following header file provides prototypes for hardware configuration
functions. */ 
#include "hwsetup.h"

/******************************************************************************
User Program Code Prototypes
******************************************************************************/ 
/* These functions are private so their prototypes are defined locally */ 
static void ConfigureCPU(void);
static void PeripheralModulesInit(void);
static void ConfigurePortPins(void);

/******************************************************************************
User Program Code
******************************************************************************/ 

/*""FUNC COMMENT""*************************************************************
* Outline	: HardwareSetup
* Description 	: Sets up the hardware.
*		  This function calls the hardware initialization functions to
*		  configure the CPU operating frequency, port pins & required
*		  on-chip modules in order to setup the RSK for the main 
*		  application.
* Argument  	: none
* Return value  : none
*""FUNC COMMENT END""*********************************************************/ 

void HardwareSetup(void) 
{
  /* Configures CPU */ 
  ConfigureCPU();

  /* Configures port pins */ 
  ConfigurePortPins();

  /* Init required on-chip peripherals */ 
  PeripheralModulesInit();
} 

/******************************************************************************
End of function HardwareSetup
******************************************************************************/ 

/*""FUNC COMMENT""*************************************************************
* Outline	: ConfigureCPU
* Description 	: Configures the core of the CPU
* Argument  	: none
* Return value  : none
*""FUNC COMMENT END""*********************************************************/ 

static void ConfigureCPU(void) 
{
  INTC.IBCR.WORD     = 0xFFFE; //Enable all of register bank
  INTC.IBNR.BIT.BE   = 3;      //Use of register banks is controlled by the setting of IBCR
  INTC.IBNR.BIT.BOVE = 1;      //Enable register bank overflow
} 

/******************************************************************************
End of function ConfigureOperatingFrequency
******************************************************************************/ 

/*""FUNC COMMENT""*************************************************************
* Outline	: ConfigurePortPins
* Description 	: Configures MCU port pins. This function configures the port 
*		  pins so that it can be used by the respective modules. 
* Argument 	: none
* Return value 	: none
*""FUNC COMMENT END""*********************************************************/ 

static void ConfigurePortPins(void) 
{
  /* Configure the port pins connected to LEDs as output port pins */ 
//  PFC.PECRL3.BIT.PE9MD = 0;
//  PFC.PEIORL.BIT.B9 = 1;
//  PFC.PECRL3.BIT.PE11MD = 0;
//  PFC.PEIORL.BIT.B11 = 1;
//  PFC.PECRL4.BIT.PE12MD = 0;
//  PFC.PEIORL.BIT.B12 = 1;
//  PFC.PECRL4.BIT.PE13MD = 0;
//  PFC.PEIORL.BIT.B13 = 1;
//  PFC.PECRL4.BIT.PE14MD = 0;
//  PFC.PEIORL.BIT.B14 = 1;
//  PFC.PECRL4.BIT.PE15MD = 0;
//  PFC.PEIORL.BIT.B15 = 1;

  /* Configure the port pins connected to LCD as output port pins */ 
//  PFC.PBCRL3.BIT.PB9MD = 0;
//  PFC.PBIORL.BIT.B9 = 1;
//  PFC.PBCRL4.BIT.PB14MD = 0;
//  PFC.PBIORL.BIT.B14 = 1;
//  PFC.PECRL1.BIT.PE0MD = 0;
//  PFC.PEIORL.BIT.B0 = 1;
//  PFC.PECRL1.BIT.PE1MD = 0;
//  PFC.PEIORL.BIT.B1 = 1;
//  PFC.PECRL1.BIT.PE2MD = 0;
//  PFC.PEIORL.BIT.B2 = 1;
//  PFC.PECRL1.BIT.PE3MD = 0;
//  PFC.PEIORL.BIT.B3 = 1;
} 

/******************************************************************************
End of function ConfigurePortPins
******************************************************************************/ 

/*""FUNC COMMENT""*************************************************************
* Outline	: PeripheralModulesInit
* Description 	: Init peripheral modules to be used by the application
* Argument  	: none
* Return value  : none
*""FUNC COMMENT END""*********************************************************/ 

static void PeripheralModulesInit(void) 
{
  /* Enable ADC0 module */ 
//  STB.CR3.BYTE &= 0xFBu;
  
  /* Init All Timer */
  TimerInit();
} 

/******************************************************************************
End of function EnablePeripheralModules
******************************************************************************/ 
